package com.example.yiwei.crm.view.activity.customerActivity;

import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.Toast;

import com.example.yiwei.crm.R;
import com.example.yiwei.crm.base.baseClass;
import com.example.yiwei.crm.data.GetContact;
import com.example.yiwei.crm.data.GetCustumer;
import com.example.yiwei.crm.entity.ContactMessageEntity;
import com.example.yiwei.crm.entity.CustomerMessageEntity;
import com.example.yiwei.crm.json.contactJson;
import com.example.yiwei.crm.json.customerJson;
import com.example.yiwei.crm.util.NetWorkUtils;
import com.example.yiwei.crm.util.SC;
import com.example.yiwei.crm.util.ZhengZhe;

import org.json.JSONException;
import org.json.JSONObject;

import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class ContactUpdateActivity extends AppCompatActivity {
    ImageView img_commt, img_back;
    EditText edit_contact_name, edit_respectful, edit_contact_address,
            edit_contact_phone,edit_contact_cphone,edit_contanct_qq,edit_contanct_weixin,edit_contanct_weibo ;
    ContactMessageEntity contact;
    String result;
    public static CustomerAddContactMessageActivity a;
    CustomerMessageEntity customer;
    private SharedPreferences sp;
    private boolean tag = false;
    String sresult;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_contact_update);
        img_back = (ImageView) findViewById(R.id.contact_add__img_back);
        img_commt = (ImageView) findViewById(R.id.contact_add_img_commit);
        edit_contact_cphone= (EditText) findViewById(R.id.contact_add_cphone);
        edit_contanct_qq = (EditText) findViewById(R.id.contact_add_edit_qq);
        edit_contanct_weixin = (EditText) findViewById(R.id.contact_add_edit_weixin);
        edit_contanct_weibo = (EditText) findViewById(R.id.contact_add_edit_webo);
        edit_contact_address = (EditText) findViewById(R.id.contact_add_edit_detailed_addresss);
        edit_contact_phone = (EditText) findViewById(R.id.contact_add_edit_mobile_phone);
        edit_contact_name = (EditText) findViewById(R.id.contact_add_edit_name);
        edit_respectful = (EditText) findViewById(R.id.contact_add_edit_respectful);
        sp = getSharedPreferences("login", Context.MODE_PRIVATE);
        contact = GetContact.get();
        setMessage();
        Intent intent = getIntent();
        result = intent.getStringExtra("spinner");
        img_commt.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if (edit_contact_phone.getText().toString().equals("")) {
                    Toast.makeText(ContactUpdateActivity.this, "请填写必填项！", Toast.LENGTH_SHORT).show();
                }else if (!ZhengZhe.phone(edit_contact_phone.getText().toString())) {
                        Toast.makeText(ContactUpdateActivity.this, "请输入正确的手机号", Toast.LENGTH_SHORT).show();
                    } else {
                        add();
                    }
            }
        });
        img_back.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                finish();
            }
        });
    }
    public void setMessage() {
        edit_contact_name.setText(contact.getContact_name());
        edit_respectful.setText(contact.getContact_respectful());
        edit_contact_phone.setText(contact.getFirst_contact_mobile_phone());
        edit_contact_address.setText(contact.getContact_address());
        edit_contact_cphone.setText(contact.getSend_contact_phone());
        edit_contanct_qq.setText(contact.getContact_qq());
//        Log.d("bb",contact.getContact_qq());
        edit_contanct_weixin.setText(contact.getContact_wechat());
        edit_contanct_weibo.setText(contact.getContact_weibo());
    }
    public void add(){
        img_commt.setVisibility(View.GONE);
        new Thread(new Runnable() {
            @Override
            public void run() {
                SC sc = new SC();
                JSONObject json=sc.storage(sp);
                customer= GetCustumer.get();
                try {
                    json.put("addoredit","edit");
                    json.put("KAA02",customer.getClient_name());
                    json.put("KAA01",customer.getClient_id());
                    json.put("KLA01",contact.getContact_id());
                    json.put("KLA06",edit_contact_phone.getText().toString());
                    json.put("KLA02",edit_contact_name.getText().toString());
                    json.put("KLA14",1);
                    json.put("KLA07",edit_contact_cphone.getText().toString());
                    json.put("KLA04",edit_respectful.getText().toString());
                    json.put("KLA10",edit_contanct_qq.getText().toString());
                    json.put("KLA11",edit_contanct_weixin.getText().toString());
                    json.put("KLA12",edit_contanct_weibo.getText().toString());
                    json.put("KLA13",edit_contact_address.getText().toString());
                } catch (JSONException e) {
                    // TODO Auto-generated catch block
                }
                String sjson=json.toString();
                //修改客户信息
                sresult = NetWorkUtils.getCount(baseClass.add_coor_customer_URL, sjson);

                runOnUiThread(new Runnable() {
                    @Override
                    public void run() {
                        if (sresult != null) {
                            try {
                                JSONObject object = new JSONObject(sresult);
                                boolean b = object.getBoolean("ifsuccess");
                                if (b) {
                                    new Thread(new Runnable() {
                                        @Override
                                        public void run() {
                                            JSONObject object1 = new JSONObject();
                                            try {
                                                object1.put("KAA01",customer.getClient_id());
                                            } catch (JSONException e) {
                                                e.printStackTrace();
                                            }
                                            GetCustumer.create();
                                            GetContact.create();
                                            String json = object1.toString();
                                            String reult = NetWorkUtils.getCount(baseClass.find_customer_msg, json);
                                            GetCustumer.customers = customerJson.jsonCustomerDetailedMessage1(reult,null);
                                            GetContact.contact = contactJson.detailedContactMessageJson1(reult,null);
                                            runOnUiThread(new Runnable() {
                                                @Override
                                                public void run() {
                                                    if (GetCustumer.customers!=null&& GetContact.contact!=null){
                                                        finish();

                                                    }

                                                }
                                            });
                                        }
                                    }).start();
                                } else {
                                    img_commt.setVisibility(View.VISIBLE);
                                    Toast.makeText(ContactUpdateActivity.this, "更新客户客户失败", Toast.LENGTH_SHORT).show();
                                }
                            } catch (JSONException e) {
                                e.printStackTrace();
                            }
                        }
                    }
                });
            }
        }).start();
    }
}

